/**
 * @Date 2025/1/8 10:50
 * @Created by 衡盛永
 */

package org.example.deploy.service.impl;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.example.deploy.mapper.SysRoleMapper;
import org.example.deploy.pojo.SysRole;
import org.example.deploy.service.SysRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

@Service
public class SysRoleServiceImpl extends ServiceImpl<SysRoleMapper, SysRole> implements SysRoleService {

    @Autowired
    private SysRoleMapper sysRoleMapper;

    @Override
    @Transactional
    public boolean assignRole(Integer roleId, Integer userId) {
        // 获取用户已分配的角色
        Integer roleIdByUserId = sysRoleMapper.getRoleIdByUserId(userId);
        // 删除已分配的角色
        boolean b = sysRoleMapper.delAssignedRole(roleIdByUserId, userId);
        if (!b) return false;
        // 分配角色
        return sysRoleMapper.assignRole(roleId, userId);
    }
}
